package com.osmap.openstreetmap.ui.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.model.LatLng;
import com.osmap.openstreetmap.R;
import com.osmap.openstreetmap.adapter.GallayAdapter;

public class ViewRealtimeOSM extends Activity implements OnClickListener {

	private static final String TAG = "ViewRealtimeOSM";

	private MapView mMapView;
	private LinearLayout mMapContent;
	private Button mShare, mEyes, mLike, mDown;
	private BaiduMap mBaiduMap;
	private Gallery mGallery;
	private GallayAdapter mAdapter;
	// 定位相关
	private LocationClient mLocClient;
	public MyLocationListenner myListener = new MyLocationListenner();
	private boolean isFirstLoc = true;// 是否首次定位

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.app_realtime_osm);
		mMapView = (MapView) findViewById(R.id.myOSMmapview);
		mBaiduMap = mMapView.getMap();

		mBaiduMap.setMyLocationEnabled(true);
		// 定位初始化
		mLocClient = new LocationClient(this);
		mLocClient.registerLocationListener(myListener);
		LocationClientOption option = new LocationClientOption();
		option.setOpenGps(true);// 打开gps
		option.setCoorType("bd09ll"); // 设置坐标类型
		option.setScanSpan(1000);
		mLocClient.setLocOption(option);
		mLocClient.start();

		mGallery = (Gallery) findViewById(R.id.image_selecotr);
		mAdapter = new GallayAdapter();
		mMapContent = (LinearLayout) findViewById(R.id.map_content);
		mLike = (Button) findViewById(R.id.btn_like);
		mEyes = (Button) findViewById(R.id.btn_show);
		mShare = (Button) findViewById(R.id.btn_share);
		mDown = (Button) findViewById(R.id.btn_down);

		mLike.setOnClickListener(this);
		mEyes.setOnClickListener(this);
		mShare.setOnClickListener(this);
		mDown.setOnClickListener(this);
		// mController = mMapView.getController();

		//
		// GeoPoint gp = new GeoPoint(39.901873, 116.326655);
		// mController.animateTo(gp, AnimationType.EXPONENTIALDECELERATING);
		// mController.setZoom(10);
		// mController.setCenter(gp);
		// mMapView.getOverlays().add(new CustomOverlay(this, null));
		// mMapView.refreshDrawableState();

	}

	/**
	 * 定位SDK监听函数
	 */
	public class MyLocationListenner implements BDLocationListener {

		@Override
		public void onReceiveLocation(BDLocation location) {
			// map view 销毁后不在处理新接收的位置
			if (location == null || mMapView == null) {
				return;
			}
			MyLocationData locData = new MyLocationData.Builder()
			.accuracy(location.getRadius())
			// 此处设置开发者获取到的方向信息，顺时针0-360
			.direction(100).latitude(location.getLatitude())
			.longitude(location.getLongitude()).build();
			mBaiduMap.setMyLocationData(locData);
			if (isFirstLoc) {
				isFirstLoc = false;
				LatLng ll = new LatLng(location.getLatitude(),
						location.getLongitude());
				MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll);
				mBaiduMap.animateMapStatus(u);
			}
		}

		public void onReceivePoi(BDLocation poiLocation) {
		}
	}

	@Override
	protected void onPause() {
		super.onPause();
		// activity 暂停时同时暂停地图控件
		mMapView.onPause();
	}

	@Override
	protected void onResume() {
		super.onResume();
		// activity 恢复时同时恢复地图控件
		mMapView.onResume();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		// activity 销毁时同时销毁地图控件
		mMapView.onDestroy();
	}

	public void showSpinner() {
		new AlertDialog.Builder(this)
		.setTitle("分享")
		.setItems(new String[] { "转发到我的[发现]", "分享到微信朋友圈", "分享到微信好友" },
				new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog,
					int which) {
				switch (which) {
				case 1:
					Toast.makeText(ViewRealtimeOSM.this,
							"已在首页地图显示此足迹！", 1 * 1000).show();
					break;
				case 2:

					break;

				case 3:

					break;

				default:
					break;
				}
			}
		}).show();

	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		switch (id) {
		case R.id.btn_like:
			Toast.makeText(ViewRealtimeOSM.this, "已将此足迹添加到收藏！", 1 * 1000)
			.show();
			break;
		case R.id.btn_down:
			// if (mMapContent.getVisibility() == View.GONE) {
			// mMapContent.setVisibility(View.VISIBLE);
			// } else {
			// mMapContent.setVisibility(View.GONE);
			// }
			break;
		case R.id.btn_share:
			showSpinner();
			break;
		case R.id.btn_show:
			Toast.makeText(ViewRealtimeOSM.this, "已在首页地图显示此足迹！", 1 * 1000)
			.show();
			break;

		default:
			break;
		}

	}

}
